<?php
namespace Home\Controller;
use Think\Controller;
class IndexController extends Controller {
    //ThinkPHP的构造函数
    public function _initialize() {
        //根据配置来开启Redis
        /*if(C('REDIS_OPEN')==1){
            //300秒
            S(array('type'=>'redis','host'=>'127.0.0.1','port'=>'6379','prefix'=>'index','expire'=>300));
        }*/
    }
    //首页
    public function index(){
        /*if(session('member_id')){
            $member_id=session('member_id');
            $member_name=session('member_name');
        }
        $data['title']='找金银网';
        $Model=D('Article');
        $Supply=D('Supply');
        $Purchase=D('Purchase');
        $Order=D("Order");
        //行业资讯
        $Model->ac_id=2;
        $Model->limit_num=5;
        $hydt_list=$Model->get_index_article_list();
        //金银动态
        $Model->ac_id=3;
        $Model->limit_num=5;
        $xydt_list=$Model->get_index_article_list();
        //活动专题
        $Model->ac_id=33;
        $Model->limit_num=3;
        $hgjj_list=$Model->get_index_article_list();
        //最新公告
        $Model->ac_id=1;
        $Model->limit_num=12;
        $xwkx_list=$Model->get_index_article_list();
        //热门资讯
        $Model->ac_id=2;
        $Model->limit_num=4;
        $rmzx_list=$Model->get_index_article_list();
        $Supply->page=1;
        $Supply->perpage=6;
        $supply_list=$Supply->get_supply_list();
        //print_r($supply_list);exit;
        $Purchase->page=1;
        $Purchase->perpage=4;
        $purchase_list=$Purchase->get_index_purchase_list();
        //订单
        $Order->page=1;
        $Order->perpage=4;
        $order_list=$Order->get_order_list();
        if(S('price_list')){
            $price_list=S('price_list');
        }else{
            $price_list=$this->get_price_list();
        }
        //print_r($price_list);exit;
        $this->assign("data", $data);
        $this->assign("hydt_list", $hydt_list);
        $this->assign("xydt_list", $xydt_list);
        $this->assign("hgjj_list", $hgjj_list);
        $this->assign("xwkx_list", $xwkx_list);
        $this->assign("rmzx_list", $rmzx_list);
        $this->assign("supply_list", $supply_list);
        $this->assign("purchase_list", $purchase_list);
        $this->assign("order_list", $order_list);
        $this->assign("member_id", $member_id);
        $this->assign("member_name", $member_name);
        $this->assign("price_list", $price_list);*/
        $Model=D('Article');
        //热门资讯
        $Model->ac_id=2;
        $Model->limit_num=5;
        $rmzx_list=$Model->get_index_article_list();
        $this->assign("rmzx_list", $rmzx_list);
        $this->display();
    }
    //获取金银价格
    private function get_price_list(){
        //抓取金银价格
        $url = "http://www.smm.cn/metals/precious";
        $ch = curl_init();
        curl_setopt ($ch, CURLOPT_URL, $url);
        curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT,10);
        $content=curl_exec($ch);
        $content=mb_convert_encoding($content,"UTF-8","GBK");
        $content=strip_tags($content);
        //1#银
        $keyword='1#银';
        $start=strpos($content,$keyword);
        $price_list[0]['name']=$keyword;
        $price_list[0]['price']=mb_substr($content,$start+50,50);
        $arr=explode('-',$price_list[0]['price']);
        $price_list[0]['minprice']=$arr[0];
        $price_list[0]['maxprice']=$arr[1];
        $price_list[0]['average']=round(($price_list[0]['minprice']+$price_list[0]['maxprice'])/2);
        $price_list[0]['change']=mb_substr($content,$start+100,50);
        //2#银
        $keyword='2#银';
        $start=strpos($content,$keyword);
        $price_list[1]['name']=$keyword;
        $price_list[1]['price']=mb_substr($content,$start+50,50);
        $arr=explode('-',$price_list[1]['price']);
        $price_list[1]['minprice']=$arr[0];
        $price_list[1]['maxprice']=$arr[1];
        $price_list[1]['average']=round(($price_list[1]['minprice']+$price_list[1]['maxprice'])/2);
        $price_list[1]['change']=mb_substr($content,$start+100,50);
        //3#银
        $keyword='3#银';
        $start=strpos($content,$keyword);
        $price_list[2]['name']=$keyword;
        $price_list[2]['price']=mb_substr($content,$start+50,50);
        $arr=explode('-',$price_list[2]['price']);
        $price_list[2]['minprice']=$arr[0];
        $price_list[2]['maxprice']=$arr[1];
        $price_list[2]['average']=round(($price_list[2]['minprice']+$price_list[2]['maxprice'])/2);
        $price_list[2]['change']=mb_substr($content,$start+100,50);
        //黄金 99.99%
        $keyword='黄金(99.99%)';
        $start+=300;
        $price_list[3]['name']=$keyword;
        $price_list[3]['price']=mb_substr($content,$start+50,50);
        $price_list[3]['average']=$price_list[3]['price'];
        $price_list[3]['change']=mb_substr($content,$start+100,50);
        //黄金 99.95%
        $keyword='黄金(99.95%)';
        $start+=300;
        $price_list[4]['name']=$keyword;
        $price_list[4]['price']=mb_substr($content,$start+50,45);
        $price_list[4]['average']=$price_list[4]['price'];
        $price_list[4]['change']=mb_substr($content,$start+100,45);
        return $price_list;
    }
    //建设中
    public function building(){
        $data['title']='找金银网';
        $this->assign("data", $data);
        $this->display();
    }
    //关于
    public function about(){
        $data['title']='关于 找金银网';
        $this->assign("data", $data);
        $this->display();
    }

    public function message_add() {
        $post=I('post.');
        if (!$post['content']) {
            echo "<script>alert('提交信息不能为空');history.back(-1);</script>";exit;
        }
        $post['upload_time']=time();
        $Model=D("Message");
        $Model->post=$post;
        if(!$Model->message_add_post()){
            echo "<script>alert('提交失败，请您尝试重新提交');history.back(-1);</script>";exit;
        }
        echo "<script>alert('提交成功');window.location.href='/home/spot';</script>";
    }

}